<?php
class IndexAction extends CommonAction {
    // 框架首页
    public function index() {
        C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
        C ( 'SHOW_PAGE_TRACE', false );
        $this->display();
    }
    
    // 检查用户是否登录
    protected function checkUser() {
    	if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
    		$this->error('没有登录','Public/login');
    	}
    }
    
    // 用户登出
    public function logout() {
    	if(isset($_SESSION[C('USER_AUTH_KEY')])) {
    		unset($_SESSION[C('USER_AUTH_KEY')]);
    		unset($_SESSION);
    		session_destroy();
    		echo json_encode (array('success' => true));
    		//$this->success('登出成功！',__URL__.'/login/');
    	}else {
//     		$this->error('已经登出！');
    		echo json_encode (array('msg' => '已经登出！'));
    	}
    }
    
    // 菜单页面
    public function menu() {
    	$this->checkUser();
    	if(isset($_SESSION[C('USER_AUTH_KEY')])) {
    		//显示菜单项
    		$menu  = array();
    		if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])) {
    			//如果已经缓存，直接读取缓存
    			$menu   =   $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
    		}else {
    			//读取数据库模块列表生成菜单项
    			$node    =   M("Node");
    			$node1   =   D("Node");
    			$id	=	$node->getField("id");
    			$list = $node1->getNodeGroupList(2,1,$id);
    			if(isset($_SESSION['_ACCESS_LIST'])) {
    				$accessList = $_SESSION['_ACCESS_LIST'];
    			}else{
    				import('@.ORG.Util.RBAC');
    				$accessList =   RBAC::getAccessList($_SESSION[C('USER_AUTH_KEY')]);
    			}
    			foreach($list as $key=>$module) {
    				if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
    					if(empty($module['name']))continue;
    					//设置模块访问权限
    					$module['access'] =   1;
    					$module['state']='open';
    					$module['attributes']= $module['name'];
    					$menu[$key]  = $module;
    				}
    			}
    			//缓存菜单访问
    			$_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]	=	$menu;
    		}
    		if(!empty($_GET['tag'])){
    			$this->assign('menuTag',$_GET['tag']);
    		}
//     		dump($menu);
//     		$menuList = menuList_to_tree($menu);
    		echo json_encode(menuList_to_tree($menu));
//     		echo json_encode($menu);
//     		$this->assign('menu',$menu);
    	}
    	C('SHOW_RUN_TIME',false);			// 运行时间显示
    	C('SHOW_PAGE_TRACE',false);
//     	$this->display();
    }
    
    // 更换密码
    public function changePwd() {
    	$this->checkUser();
    	//对表单提交处理进行处理或者增加非表单数据
    	if(md5($_POST['verify'])	!= $_SESSION['verify']) {
    		$this->error('验证码错误！');
    	}
    	$map	=	array();
    	$map['password']= pwdHash($_POST['oldpassword']);
    	if(isset($_POST['account'])) {
    		$map['account']	 =	 $_POST['account'];
    	}elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
    		$map['id']		=	$_SESSION[C('USER_AUTH_KEY')];
    	}
    	//检查用户
    	$User    =   M("User");
    	if(!$User->where($map)->field('id')->find()) {
    		echo json_encode (array('errorMsg'=>'修改出错！'));
    	}else {
    		$User->password	=	pwdHash($_POST['password']);
    		$User->save();
    		echo json_encode (array('success'=>true));
    	}
    }
    
    public function verify() {
    	$type	 =	 isset($_GET['type'])?$_GET['type']:'gif';
    	import("@.ORG.Util.Image");
    	Image::buildImageVerify(4,1,$type);
    }
    
    public function profile() {
    	$this->checkUser();
    	$User	 =	 M("User");
    	$vo	=	$User->getById($_SESSION[C('USER_AUTH_KEY')]);
    	echo json_encode ($vo);
//     	$this->assign('vo',$vo);
//     	$this->display();
    }
    
    // 修改资料
    public function changeProfile() {
    	$this->checkUser();
    	$User	 =	 D("User");
    	if(!$User->create()) {
    		$this->error($User->getError());
    	}
    	$result	=	$User->save();
    	if(false !== $result) {
    		echo json_encode (array('success'=>true));
    	}else{
    		echo json_encode (array('errorMsg'=>'修改出错！'));
    	}
    }
}